package com.qingcloud.adminbackend.controller;

import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.source.DataSourceDatabase;
import com.qingcloud.adminbackend.service.IDatabaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author qym
 * @Description 数据库管理api
 * @Date 下午4:33 2021/3/29
 **/
@RestController
@RequestMapping("/database")
public class TdDatabaseController {

    @Resource
    private IDatabaseService dataSourceDatabaseService;

    @ApiOperation(value = "新增数据库", notes = "新增数据库")
    @PostMapping("/add")
    public AjaxResult addDatabase(@RequestBody DataSourceDatabase dataSource) {
        return dataSourceDatabaseService.addDatabase(dataSource);
    }

    @ApiOperation(value = "删除数据库", notes = "删除数据库")
    @GetMapping("/deleteByIds")
    public AjaxResult deleteDatabaseByIds(@RequestParam("ids") String ids) {
        return dataSourceDatabaseService.deleteDatabaseByIds(ids);
    }

    @ApiOperation(value = "修改数据库", notes = "修改数据库")
    @PostMapping("/update")
    public AjaxResult updateDatabase(@RequestBody DataSourceDatabase dataSource) {
        return dataSourceDatabaseService.updateDatabase(dataSource);
    }

    @ApiOperation(value = "查询数据库指定id", notes = "查询数据库指定id", response = DataSourceDatabase.class)
    @GetMapping("/findByIds")
    public AjaxResult<List<DataSourceDatabase>> findDatabaseByIds(@RequestParam("ids") String ids) {
        return dataSourceDatabaseService.findDatabaseByIds(ids);
    }

    @ApiOperation(value = "模糊查询数据库指定catalog名称", notes = "模糊查询数据库指定catalog名称", response = DataSourceDatabase.class)
    @GetMapping("/findByName")
    public AjaxResult<List<DataSourceDatabase>> findDatabaseByName(@RequestParam("catalog") String catalog) {
        return dataSourceDatabaseService.findDatabaseByName(catalog);
    }

    @ApiOperation(value = "获取数据库详情", notes = "获取数据库详情id", response = String.class)
    @GetMapping("/getDatabaseByDsId")
    public AjaxResult getDatabaseByDsId(@RequestParam("dsId") Integer dsId) {
        if (dsId == null) return new AjaxResult<>(WamingConstant.SUCCESS, "dsId参数不能为空", "");
        DataSourceDatabase database = dataSourceDatabaseService.getDatabaseByDsId(dsId);
        if (database == null) return new AjaxResult<>(WamingConstant.SUCCESS, "无记录", "");
        else return new AjaxResult<>(WamingConstant.SUCCESS, "", database);
    }

    @ApiOperation(value = "批量获取数据库详情", notes = "批量获取数据库详情", response = String.class)
    @GetMapping("/getDatabaseByDsIds")
    public AjaxResult getDatabaseByDsId(@RequestParam("dsIds") String dsIds) {
        return new AjaxResult<>(WamingConstant.SUCCESS, "", dataSourceDatabaseService.getDatabaseByDsIds(dsIds));
    }

    @ApiOperation(value = "获取数据库所有类型", notes = "获取数据库所有类型", response = String.class)
    @GetMapping("/getTypes")
    public AjaxResult getTypes() {
        return dataSourceDatabaseService.getTypes();
    }
}
